<?php

namespace catchAdmin\system\model;


use app\common\model\BaseModel;
use catcher\base\CatchModel as Model;

/**
 *
 * @property int $id
 * @property string $device_code
 * @property string $phone
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 * @property int $creator_id
 */
class AppSetting extends BaseModel
{

    public $name = "app_setting";
    public $field = [
        //
        'id',
        // 设备码
        'device_code',
        // 本机号码
        'phone',
        // 创建时间
        'created_at',
        // 更新时间
        'updated_at',
        // 软删除字段
        'deleted_at',
        // 创建人ID
        'creator_id',
        // 拨打电话方式  1 电话机，2 语音盒
        'call_method',
        'local_prefix',
        'outer_prefix'
    ];

    public function getDeviceInfo(string $deviceCode)
    {
        return $this->where([["device_code", "=", $deviceCode]])->findOrEmpty();
    }

    public function saveDevice(array $data): bool
    {
        $deviceInfo = $this->where([["device_code", "=", $data["device_code"]]])->find();
        if (!empty($deviceInfo)) {
            $result = $this->where([["device_code", "=", $data["device_code"]]])->update([
                'phone' => $data['phone'],
                'call_method' => (int)$data['call_method'],
                'local_prefix' => $data['local_prefix'] ?? "",
                'outer_prefix' => $data['outer_prefix'] ?? "",
                'LocalAreaCode'=>$data['LocalAreaCode']??"",
                'local_landline'=>$data['local_landline']??"",
                'outer_landline'=>$data['outer_landline']??"",

            ]);
            return $result !== false;
        }
        return (bool)$this->createBy([
            'device_code' => $data['device_code'],
            'phone' => $data['phone'],
            'call_method' => (int)$data['call_method'],
            'deleted_at' => 0,
            'local_prefix' => $data['local_prefix'] ?? "",
            'outer_prefix' => $data['outer_prefix'] ?? "",
            'LocalAreaCode'=>$data['LocalAreaCode']??"",
            'local_landline'=>$data['local_landline']??"",
            'outer_landline'=>$data['outer_landline']??"",
        ]);
    }

}